//
//  ST_String+Bt.swift
//  BTVideo
//
//  Created by DouLei on 2019/7/20.
//  Copyright © 2019 DouLei. All rights reserved.
//

import Foundation
import SwifterSwift







extension String {
    
    
    func size(withConstraintRect constraintRect: CGSize, font: UIFont) -> CGSize{
        
        let boundingBox = self.boundingRect(with: constraintRect, options: .usesLineFragmentOrigin, attributes: [NSAttributedString.Key.font: font], context: nil)
        
        return CGSize.init(width: boundingBox.size.width + 2, height: boundingBox.size.height + 2)
        
    }
    
    
    
    func height(withConstrainedWidth width: CGFloat, font: UIFont) -> CGFloat {
        let constraintRect = CGSize(width: width, height: .greatestFiniteMagnitude)
        let boundingBox = self.boundingRect(with: constraintRect, options: .usesLineFragmentOrigin, attributes: [NSAttributedString.Key.font: font], context: nil)
        
        return ceil(boundingBox.height) + 1
    }
    
    func width(withConstrainedHeight height: CGFloat, font: UIFont) -> CGFloat {
        let constraintRect = CGSize(width: .greatestFiniteMagnitude, height: height)
        let boundingBox = self.boundingRect(with: constraintRect, options: .usesLineFragmentOrigin, attributes: [NSAttributedString.Key.font: font], context: nil)
        
        return ceil(boundingBox.width) + 2
    }
    
    func removingWhitespaces() -> String {
        return components(separatedBy: .whitespaces).joined()
    }
    
    
    func load_image_from_disk() -> UIImage? {
        
        if self == ""{
            return nil
        }
        
        do {
            //            print("++++++Load Image URL String:",filePath)
            let fileURL = URL.init(fileURLWithPath: self)
            //            print("FILE--URL-1:",fileURL)
            
            let imageData = try Data(contentsOf: fileURL)
            return UIImage(data: imageData)
        } catch {
            print("Error loading image : \(error)--线程:",Thread.current)
        }
        
        return nil
    }
    
    func load_data_from_disk() -> Data? {
        
        if self == ""{
            return nil
        }
        
        do {
            //            print("++++++Load Image URL String:",filePath)
            let fileURL = URL.init(fileURLWithPath: self)
            //            print("FILE--URL-1:",fileURL)
            
            let data = try Data(contentsOf: fileURL)
            return data
        } catch {
            print("Error loading Data : \(error)--线程:",Thread.current)
        }
        
        return nil
    }
    
    
}


extension NSAttributedString {
    
    func height(withConstrainedWidth width: CGFloat) -> CGFloat {
        
        let constraintRect = CGSize(width: width, height: .greatestFiniteMagnitude)
        let boundingBox = boundingRect(with: constraintRect, options: .usesLineFragmentOrigin, context: nil)
        
        return ceil(boundingBox.height) + 2
    }
    
    func width(withConstrainedHeight height: CGFloat) -> CGFloat {
        let constraintRect = CGSize(width: .greatestFiniteMagnitude, height: height)
        let boundingBox = boundingRect(with: constraintRect, options: .usesLineFragmentOrigin, context: nil)
        
        return ceil(boundingBox.width) + 2
    }
}
